var express = require('express')
var router = express.Router()
var mongoose = require('mongoose')
var User = require('./../models/user')
var connection = mongoose.connect('mongodb://127.0.0.1:27017/test', {
	useNewUrlParser: true
})
require('./../util/util')
mongoose.connection.on('connected', () => {
	console.log('success2');
})
router.post('/login', (req, res, next) => {
	// console.log(req.body);
	var param = {
		userName: req.body.userName,
		userPwd: req.body.userPwd
	}
	User.findOne(param, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err
			})
		} else {
			if (doc) {
				res.cookie("userId", doc.userId, {
					path: "/",
					maxAge: 1000 * 60 * 60
				});
				res.cookie("userName", doc.userName, {
					path: "/",
					maxAge: 1000 * 60 * 60
				});
				res.json({
					status: "0",
					msg: "",
					result: {
						userName: doc.userName
					}

				})
			}

		}
	})
})
//登出
router.post("/logout", (req, res, next) => {
	res.cookie("userId", "", {
		path: "/",
		maxAge: -1
	});
	res.json({
		status: "0",
		msg: "",
		result: ""
	})
})
//
router.get("/checkLogin", (req, res, next) => {
	if (req.cookies.userId) {
		res.json({
			status: "0",
			msg: "",
			result: req.cookies.userName
		})
	} else {
		res.json({
			status: "1",
			msg: "未登录",
			result: ""
		})
	}

})


//购物车列表
router.get('/cartList', (req, res, next) => {
	let userId = req.cookies.userId
	User.findOne({
		userId
	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			if (doc) {
				doc.cartList
				res.json({
					status: "0",
					msg: '',
					result: doc.cartList
				})
			}
		}
	})
});

//购物车删除
router.post('/cartDel', (req, res, next) => {
	let userId = req.cookies.userId
	let productId = req.body.productId
	User.update({
		userId
	}, {
		$pull: {
			'cartList': {
				'productId': productId
			}
		}

	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			res.json({
				status: "0",
				msg: '',
				result: "suc"
			})
		}
	})

})

//
router.post('/cartEdit', (req, res, next) => {
	let userId = req.cookies.userId
	let productId = req.body.productId
	let productNum = req.body.productNum
	let checked = req.body.checked
	User.update({
		userId,
		"cartList.productId": productId
	}, {
		"cartList.$.productNum": productNum,
		"cartList.$.checked": checked
	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			res.json({
				status: "0",
				msg: '',
				result: "suc"
			})
		}
	})


})
router.post('/editCheckAll', (req, res, next) => {
	let userId = req.cookies.userId
	let checkAll = req.body.checkAll ? '1' : '0'
	User.findOne({
		userId
	}, (err, user) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			if (user) {
				user.cartList.forEach((item, index) => {
					item.checked = checkAll
				})
				user.save((err1, doc) => {
					if (err) {
						res.json({
							status: "1",
							msg: err.message,
							result: ""
						})
					} else {
						res.json({
							status: "0",
							msg: "",
							result: "suc"
						})
					}
				})
			}

		}
	})
})
// 地址列表
router.get('/addressList', (req, res, next) => {
	let userId = req.cookies.userId
	User.findOne({
		userId
	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			res.json({
				status: "0",
				msg: '',
				result: doc.addressList
			})
		}
	})

})

router.post('/setDefault', (req, res, next) => {
	let userId = req.cookies.userId
	let addressId = req.body.addressId
	User.findOne({
		userId
	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			let addressList = doc.addressList;
			addressList.forEach((item) => {
				if (item.addressId == addressId) {
					item.isDefault = true
				} else {
					item.isDefault = false
				}
			})
			doc.save((err1, doc1) => {
				if (err1) {
					res.json({
						status: "1",
						msg: err.message,
						result: ""
					})
				} else {
					res.json({
						status: "0",
						msg: '',
						result: 'suc'
					})
				}
			})
		}
	})
})

//删除地址
router.post('/delAddress', (req, res, next) => {
	let userId = req.cookies.userId
	let addressId = req.body.addressId
	User.update({
		userId
	}, {
		$pull: {
			'addressList': {
				'addressId': addressId
			}
		}

	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			res.json({
				status: "0",
				msg: '',
				result: "suc"
			})
		}
	})

})

router.post('/payMent', (req, res, next) => {
	let userId = req.cookies.userId;
	let orderTotal = req.body.orderTotal;
	let addressId = req.body.addressId;
	User.findOne({
		userId
	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			let address = "";
			let goodList = [];

			//获取当前用户地址信息
			doc.addressList.forEach((item) => {
				if (addressId == item.addressId) {
					address = item.addressId
				}
			})
			//获取用户购物车购买商品
			doc.cartList.filter((item) => {
				if (item.checked == "1") {
					goodList.push(item)
				}
			})
			let platform = "622";
			let r1 = Math.floor(Math.random() * 10)
			let r2 = Math.floor(Math.random() * 10)
			let sysDate = new Date().Format('yyyyMMddhhmmss')
			let createDate = new Date().Format('yyyy-MM-dd hh:mm:ss')
			let orderId = platform + r1 + sysDate + r2;
			let order = {
				orderId,
				orderTotal,
				addressInfo: address,
				goodList,
				orderStatus: "1",
				createDate: ""
			}
			doc.orderList.push(order)
			doc.save((err1, doc1) => {
				if (err) {
					res.json({
						status: "1",
						msg: err.message,
						result: ""
					})
				}
			})
			res.json({
				status: "0",
				msg: "",
				result: {
					orderId: order.orderId,
					orderTotal: order.orderTotal
				}
			})
		}
	})

})


router.get('/orderDeatail', (req, res, next) => {
	let userId = req.cookies.userId;
	let orderId = req.param("orderId");
	User.findOne({
		userId
	}, (err, doc) => {
		if (err) {
			res.json({
				status: "1",
				msg: err.message,
				result: ""
			})
		} else {
			let orderList = doc.orderList
			console.log(orderList.length)
			if (orderList.length > 0) {
				let orderTotal = "";
				orderList.forEach((item) => {
					if (item.orderId == orderId) {
						orderTotal = item.orderTotal
					}
				})
				res.json({
					status: "0",
					msg: '',
					result: {
						orderId,
						orderTotal

					}
				})

			} else {
				res.json({
					status: "1200001",
					msg: '无此订单',
					result: ""
				})

			}
		}
	})

})

router.get('/cartCount', (req, res, next) => {
	if (req.cookies && req.cookies.userId) {
		let userId = req.cookies.userId;
		User.findOne({
			userId
		}, (err, doc) => {
			if (err) {
				res.json({
					status: "1",
					msg: err.message,
					result: ""
				})
			} else {
				let cartList = doc.cartList
				let cartCount = 0;
				cartList.forEach((item) => {
					cartCount += parseInt(item.productNum)
				})
				res.json({
					status: '0',
					msg: '',
					result: cartCount
				})
			}
		})

	}

})

module.exports = router